//
//  ByBlueToothMeaturesS1Model.m
//  laserdevice_ios
//
//  Created by pro on 16/12/12.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByBlueToothMeaturesS1Model.h"

@implementation ByBlueToothMeaturesS1Model

- (instancetype)initWithDataHexString:(NSString *)hexStr {
    
    if (self = [super initWithDataHexString:hexStr]) {
        
//        self.name = @"S1模型";
        self.name = [NSString stringWithFormat:@"S1%@", STRING(@"moxing")];

        NSString *lengthStr1 = [hexStr substringWithRange:NSMakeRange(42, 18)];
        self.length1 = [[self getNumberFromHexStr:lengthStr1] floatValue];
        NSString *lengthStr2 = [hexStr substringWithRange:NSMakeRange(70, 18)];
        self.length2 = [[self getNumberFromHexStr:lengthStr2] floatValue];
        NSString *areaStr = [hexStr substringWithRange:NSMakeRange(98, 18)];
        self.area = [[self getNumberFromHexStr:areaStr] floatValue];

        NSLog(@"%li,%.2f,%.2f,%.2f", self.numOfData, self.length1, self.length2, self.area);
    }
    return self;
}

- (instancetype)initWithJsonDic:(NSDictionary *)jsonDic {
    
    if (self = [super initWithJsonDic:jsonDic]) {
        
        NSArray *attributes = jsonDic[@"attributes"];
        self.length1 = [attributes[0] floatValue];
        self.length2 = [attributes[1] floatValue];
        self.area = [attributes[2] floatValue];
    }
    return self;
}


- (NSDictionary *)saveDic {
    
    NSArray *attributes = @[[NSString stringWithFormat:@"%.6lf", self.length1],
                            [NSString stringWithFormat:@"%.6lf", self.length2],
                            [NSString stringWithFormat:@"%.6lf", self.area],];
    return @{@"name"        :self.name,
             @"attributes"  :attributes,};
}

- (NSString *)length1Str {
    
    return [self getStringFromLength:self.length1];
}
- (NSString *)length2Str {
    
    return [self getStringFromLength:self.length2];
}
- (NSString *)areaStr {
    
    int unit = [ByCommonCache getSystemUnit].intValue;
    
    NSString *l1s, *l2s;
    switch (unit) {
        case 0: // 米
            l1s = [NSString stringWithFormat:@"%.3lf", _length1];
            l2s = [NSString stringWithFormat:@"%.3lf", _length2];
            break;
        case 1: // 英尺
        case 2: // 英寸
        case 3: // 英尺+英寸
            l1s = [NSString stringWithFormat:@"%.3lf", _length1 * 3.2808399];
            l2s = [NSString stringWithFormat:@"%.3lf", _length2 * 3.2808399];
            break;
        default:
            break;
    }
    _area = l1s.floatValue * l2s.floatValue;
    return [NSString stringWithFormat:@"%.3f", _area];
}


@end
